package com.xgp.microChat.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xgp.microChat.model.User;
import com.xgp.microChat.service.UserService;
import com.xgp.microChat.tool.DataGridView;
import com.xgp.microChat.tool.HttpMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 咸鱼
 * @since 2020-04-22
 */
@Api(tags = "用户相关")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Value("${user.password.head}")
    private String password_head;

    @Value("${user.password.foot}")
    private String password_foot;

    @Value("${server.port}")
    private int serverPort;

    @ApiOperation("用户注册方法")
    @PostMapping("/register")
    public DataGridView<String> register(@RequestParam String username,
                          @RequestParam String password,
                          @RequestParam String email) {

        User user = new User();
        user.setUsername(username);
        //第一个加密应该是在前端做的，这里仅仅只是为了测试
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        password = DigestUtils.md5DigestAsHex((password_head + password + password_foot).getBytes());
        user.setPassword(password);
        user.setEmail(email);

        int flag = userService.insertUser(user);
        //使用断言，因为注册用户一定要成功
        Assert.isTrue(flag == 1,"新增用户失败：flag == " + flag);
        //统一放回值格式
        return new DataGridView<String>(true,"ok");
    }
}

